import * as service from '../../services/billService';
import { BILLTYPE } from '../../utils/types';
export default {
  namespace: 'businessMgr',
  state: {
    billType: '',
    pagePath: '',
    list: {
      queryStr: '',
      queryKey: '未完成',
      selectedItemIds: [],
      columns: [],
      items: [],
      page: 1,
      pageSize: 10,
      totalCount: 0
    }
  },
  reducers: {
    changeBillType(state, action) {
      return { ...state, page: 1,billType: action.payload };
    },
    changeQueryStr(state, action) {
      return { ...state, list: { ...state.list, page: 1, queryStr: action.payload } };
    },
    changeQueryKey(state, action) {
      return { ...state, list: { ...state.list, page: 1, queryKey: action.payload } };
    },
    changeColumns(state, action) {
      return { ...state, list: { ...state.list, columns: action.payload } };
    },
    changePage(state, { payload: { page = 1, pageSize = 10 } }) {
      return { ...state, list: { ...state.list, page: page, pageSize: pageSize } };
    },
    changeSelectedItemIds(state, action) {
      debugger;
      return { ...state, list: { ...state.list, selectedItemIds: action.payload } };
    },
    updatePath(state, action) {
      return { ...state, pagePath: action.payload };
    },
    updateList(state, { payload: { items, totalCount } }) {
      return { ...state, list: { ...state.list, items: items, totalCount: totalCount } };
    }
  },
  effects: {
    *initColumns({ payload }, { select, call, put }) {
      const curBillType = yield select(state => state.businessMgr.billType);
      if (curBillType == payload)//相同类型，已加载过
        return;
      const currentClmConfig = yield call(service.getClmConfig, { billType: payload });
      yield put({
        type: 'changeColumns',
        payload: currentClmConfig
      })
    },
    *query({ }, { select, call, put }) {
      const state = yield select(state => state.businessMgr);
      const res = yield call(service.getList, {
        billType: state.billType,
        page: state.list.page,
        pageSize: state.list.pageSize,
        queryStr: state.list.queryStr,
        queryKey: state.list.queryKey
      });
      if (res === undefined || res.result == null) {
        return;
      }
      else {
        yield put({
          type: 'updateList',
          payload: {
            items: res.result.items,
            totalCount: res.result.totalCount,
          }
        })
      }
    },
    *queryBySearch({ payload }, { put }) {
      yield put({
        type: 'changeQueryStr',
        payload: payload
      })
      yield put({
        type: 'query'
      })
    },
    *queryByKey({ payload }, { put }) {
      yield put({
        type: 'changeQueryKey',
        payload: payload
      })
      yield put({
        type: 'query'
      })
    },
    *queryByPage({ payload }, { put }) {
      yield put({
        type: 'changePage',
        payload: payload
      })
      yield put({
        type: 'query',
        payload: payload
      })
    },
    *remove({ }, { select, call, put }) {
      const ids = yield select(state => state.businessMgr.list.selectedItemIds);
      debugger;
      for (var i = 0; i < ids.length; i++) {
        yield call(service.remove, ids[i]);
      }

      yield put({
        type: 'query'
      })
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        const paths = pathname.split('/');
        if (paths.length > 2) {
          const menuName = paths[3];
          let path = '', billType = '';
          switch (menuName) {
            case BILLTYPE.生产单:
              path = '单据管理-生产单';
              billType = 'T1';
              break;
            case BILLTYPE.入库单:
              path = '单据管理-入库单';
              billType = 'T2';
              break;
            case BILLTYPE.出库单:
              path = '单据管理-出库单';
              billType = 'T3';
              break;
            default:
              path = '';
              break;
          }
          dispatch({
            type: 'initColumns',
            payload: billType
          })
          dispatch({
            type: 'updatePath',
            payload: path
          })
          dispatch({
            type: 'changeBillType',
            payload: billType
          })
          dispatch({
            type: 'query',
            payload: ''
          })
        }
        else {
          dispatch({
            type: 'updatePath',
            payload: '业务管理'
          })
        }
      })
    }
  },
};
